#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using System.Linq;
using AzureProvider.SoupMethods;

#endregion

namespace BlogEngine.Core.Providers
{
	/// <summary>
	/// A storage provider for BlogEngine that uses XML files.
	/// <remarks>
	/// To build another provider, you can just copy and modify
	/// this one. Then add it to the web.config's BlogEngine section.
	/// </remarks>
	/// </summary>
    public partial class AzureBlogProvider : BlogProvider
	{
		//private static string _Folder = System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation);

        PostsMethods PostsMethods = new PostsMethods();
        PostTagMethods PostTagMethods = new PostTagMethods();
        PostCommentMethods PostCommentMethods = new PostCommentMethods();
        PostCategoryMethods PostCategoryMethods = new PostCategoryMethods();
        PostNotifyMethods PostNotifyMethods = new PostNotifyMethods();
		

		/// <summary>
		/// Retrieves a post based on the specified Id.
		/// </summary>
		public override Post SelectPost(Guid id)
		{
            PostsMethods PostsMethods = new PostsMethods();
            Post post = (Post) PostsMethods.GetObject(id.ToString(), out errMessage);
            PostsMethods.FillRelated(ref post, out errMessage);
            //pour retrouver le parent du comment
            post.Comments.ForEach(act => act.Parent = post);
            
			return post;
		}

		///// <summary>
		///// Retrieves the content of the post in order to lazy load.
		///// </summary>
		///// <param name="id"></param>
		///// <returns></returns>
		//public override string SelectPostContent(Guid id)
		//{
		//  string fileName = _Folder + "posts" + Path.DirectorySeparatorChar + id.ToString() + ".xml";
		//  Post post = new Post();
		//  XmlDocument doc = new XmlDocument();
		//  doc.Load(fileName);

		//  if (doc.SelectSingleNode("post/content") != null)
		//    return doc.SelectSingleNode("post/content").InnerText;

		//  return string.Empty;
		//}

		/// <summary>
		/// Inserts a new Post to the data store.
		/// </summary>
		/// <param name="post"></param>
        public override void InsertPost(Post post)
        {

            PostsMethods.Insert(post, out errMessage);

            UpdatePostRellated(post);
        }

		/// <summary>
		/// Updates a Post.
		/// </summary>
		public override void UpdatePost(Post post)
		{
            PostsMethods.Update(post, out errMessage);

            UpdatePostRellated(post);
		}

        private void UpdatePostRellated(Post post)
        {
            PostTagMethods.Update(post.Id.ToString(), PostTagMethods.CreatePostTag(post.Id.ToString(), post.Tags.ToArray()), out errMessage);
            PostCommentMethods.Update(post.Id.ToString(), post.Comments.ToArray(), out errMessage);
            PostCategoryMethods.Update(post.Id.ToString(), 
                PostCategoryMethods.CreatePostCategory(post.Id.ToString() , post.Categories.ToArray()), out errMessage);

            string[] notify = new string[post.NotificationEmails.Count];
            post.NotificationEmails.CopyTo(notify, 0);
            PostNotifyMethods.Update(post.Id.ToString(), PostNotifyMethods.CreatePostTag(post.Id.ToString(), notify), out errMessage);
        }

        

		/// <summary>
		/// Deletes a post from the data store.
		/// </summary>
		public override void DeletePost(Post post)
		{
            PostsMethods.DeletePost(post.Id.ToString(),out errMessage );
		}

		/// <summary>
		/// Retrieves all posts from the data store
		/// </summary>
		/// <returns>List of Posts</returns>
		public override List<Post> FillPosts()
		{
            List<Post> posts = new List<Post>();
            posts = PostsMethods.GetPosts(null, out errMessage).ToList<Post>();
            for (int i = 0; i < posts.Count;i++ )
            {
                Post post = posts[i];
                PostsMethods.FillRelated(ref post, out errMessage);
                //pour retrouver le parent du comment
                post.Comments.ForEach(act => act.Parent = post);
            
                posts[i] = post;
                posts[i].MarkOld();
            }
            posts.Sort();

            return posts;
		}

	}
}